#include "ST_MainMenu.h"
#include "Game/Game.h"
#include "Game/dConfig.h"
#include "Player/OnceFac.h"
#include "Player/LoopFac.h"
#include "GameRes/AnimMgr.h"
#include "Game/SoundPlayer.h"
#include "Game/SoundDef.h"
#include "SimpleAudioEngine.h"
#include <memory>
namespace LTGame{

	ST_MainMenu::ST_MainMenu(GST_FLAG flag)
		: GameState(flag)
	{
		mm_index = MM_START;
		speed = 0;
		AnimMgr::getInstance()->LoadByID(3);
		CCGame->m_uiData->openFrame(Form_MainMenu);
		auto_ptr<AnimPlayerFac> fac(new OnceFac());
		mm_player = fac->CreatePlayer(3, 0);
		mm_img = CCSprite::spriteWithFile("gui_pic/fengmian.png");
		mm_img->setScaleX(CCGame->ScaleX);
		mm_img->setScaleY(CCGame->ScaleY);
		//mm_img->setPosition(ccp(S_WIDTH_HALF*mm_img->getScaleX(), S_HEIGHT_HALF*mm_img->getScaleY()));
		mm_img->retain();	
		mm_img1 = CCSprite::spriteWithFile("gui_pic/fengmian.png");
		mm_img1->setScaleX(CCGame->ScaleX);
		mm_img1->setScaleY(CCGame->ScaleY);
		//mm_img1->setPosition(ccp(S_WIDTH_HALF*mm_img->getScaleX(), S_HEIGHT_HALF*mm_img->getScaleY()));
		mm_img1->retain();	
		if (CCGame->isOpenMusic)
			SoundPlayer::getInstance()->playBkMain(SND_COVER);

	}

	ST_MainMenu::~ST_MainMenu(void)
	{
		AnimMgr::getInstance()->freeRes();
		CC_SAFE_DELETE(mm_img);
		CC_SAFE_DELETE(mm_img1);
		CC_SAFE_DELETE(mm_player);
	}

	bool ST_MainMenu::logic()
	{
		int block[4];
		CCGame->m_uiData->getBlock(Form_MainMenu,B_Form_Block_2,block);
		if (speed>=S_HEIGHT)
		{
			speed = 0;
		}
		else{
			speed+=0.1;
		}
		mm_img->setPosition(ccp(S_WIDTH_HALF*mm_img->getScaleX(), S_HEIGHT_HALF*mm_img->getScaleY()+S_HEIGHT-speed-1));
		mm_img->visit();
		mm_img1->setPosition(ccp(S_WIDTH_HALF*mm_img->getScaleX(), S_HEIGHT_HALF*mm_img->getScaleY()-speed));
		mm_img1->visit();
		CCGame->m_uiData->drawFrame(Form_MainMenu);
		mm_player->drawFrame(block[0]+block[2]/2,block[1]+block[3]/2, false);
		mm_player->updateActionSquenceController();

		return true;
	}
	bool ST_MainMenu::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
	{
		CCPoint point = pTouch->locationInView( pTouch->view() );
		return true;
	}
	void ST_MainMenu::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
	{
		CCPoint point = pTouch->locationInView( pTouch->view() );
		if ( CCGame->m_uiData->isPointerInBlock(point.x, point.y, Form_MainMenu, B_Form_Block_3) )
		{
			if (CCGame->isOpenMusic)
				SOUND_PLAY_EFF(SND_OK);
			CCGame->SetState(GST_GameSelLevel);
		}
		if ( CCGame->m_uiData->isPointerInBlock(point.x, point.y, Form_MainMenu, B_Form_Block_4) )
		{
			if (CCGame->isOpenMusic)
				SOUND_PLAY_EFF(SND_OK);
			CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
			CCDirector::sharedDirector()->end();
		}
	}

	void ST_MainMenu::doPressLogic()
	{
		switch(mm_index)
		{
		case MM_START:
			CCGame->SetState(GST_GameSelLevel);
			break;
		case MM_RANKING:
			CCGame->SetState(GST_Ranking);
			break;
		case	MM_SET:
			CCGame->SetState(GST_SetMusic);
			break;
		case	MM_HELP:
			CCGame->SetState(GST_Help);
			break;
		case	MM_ABOUT:
			CCGame->SetState(GST_About);
			break;
		case	MM_SHUTDOMN:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
			CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
			exit(0);
#endif
			break;
		case	MM_TWITTER:
			break;
		case	MM_FACEBOOK:
			//CCGame->SetState(GST_GamePass);
			break;
		}
	}

}